# Zope imports
from AccessControl import ClassSecurityInfo

# zope imports
from zope.interface import implements

# Archetypes imports
from Products.Archetypes.atapi import *

# ATContentTypes imports
from Products.ATContentTypes.configuration import zconf
from Products.ATContentTypes.content.base import ATCTMixin
from Products.ATContentTypes.content.schemata import finalizeATCTSchema

# EasyArticle imports
from easyarticle.core.config import *
from easyarticle.core.interfaces import IEasyArticle

ArticleSchema = ATCTMixin.schema.copy() + Schema((
    TextField('text',
              required=False,
              searchable=True,
              primary=True,
              storage = AnnotationStorage(migrate=True),
              validators = ('isTidyHtmlWithCleanup',),
              #validators = ('isTidyHtml',),
              default_content_type = zconf.ATDocument.default_content_type,
              default_output_type = 'text/x-html-safe',
              allowable_content_types = zconf.ATDocument.allowed_content_types,
              widget = RichWidget(
                        description = "",
                        description_msgid = "help_body_text",
                        label = "Body Text",
                        label_msgid = "label_body_text",
                        rows = 25,
                        i18n_domain = "EasyArticle",
                        allow_file_upload = zconf.ATDocument.allow_document_upload)),
    ), marshall=RFC822Marshaller()
    )

finalizeATCTSchema(ArticleSchema)

# First approach was with AllowedTypesByIfaceMixin, but without it is possiple
# to adapt the special needs (add remove addable types) for certain 
# customers/use cases by GS. Additionally AllowedTypesByIfaceMixin seems not
# to work with the restrict types thingy.
class EasyArticle(OrderedBaseFolder, ATCTMixin):
    """
    """
    __implements__ = (ATCTMixin.__implements__)
    implements(IEasyArticle)

    security = ClassSecurityInfo()    
    _at_rename_after_creation = True        
    schema = ArticleSchema

    displayContentsTab = False
    
    # Note: this is also called from all sub objects via the reindexObject()
    # method. This means if you change a sub object it will be hidden from 
    # the catalog and its SearchableText will be added to the Article.
    def SearchableText(self):
        """
        """
        result = [
            super(EasyArticle, self).SearchableText()
        ]
        
        for object in self.objectValues():
            try:
                result.append(object.SearchableText())
            except AttributeError:
                pass
            
            # Note: If object is an EAReference only the reference will be
            # unindexed not the referenced object.
            object.unindexObject()
                
        return " ".join(result)

registerType(EasyArticle, PROJECTNAME)